package com.ssm.controller;

import java.io.*;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssm.pojo.InfoVo;
import com.ssm.util.ExportExcelUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class UploadAndDownAction {
    /**
     * 上传多个附件的操作类
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/user/upload", method = RequestMethod.POST)
    @ResponseBody
    public void doUploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {

        if (!file.isEmpty()) {
            try {
                // 这里将上传得到的文件保存指定目录下
                FileUtils.copyInputStreamToFile(file.getInputStream(),
                        new File("d:\\upload\\file\\", System.currentTimeMillis() + file.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        InputStream in = null;
        List<List<Object>> listob = null;
        in = file.getInputStream();
        listob = new ExportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
        // 该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            InfoVo vo = new InfoVo();
            vo.setCode(String.valueOf(lo.get(0)));
            vo.setName(String.valueOf(lo.get(1)));
            vo.setDate(String.valueOf(lo.get(2)));
            vo.setMoney(String.valueOf(lo.get(3)));
            System.out.println("打印信息-->机构:" + vo.getCode() + "  名称：" + vo.getName() + "   时间：" + vo.getDate() + "   资产："
                    + vo.getMoney());
        }

        //return "success"; // 上传成功则跳转至此success的信息
    }

    @RequestMapping(value="/download",method = RequestMethod.GET)
    public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/");
        //文件名
        String fileName="template.xlsx";
        //设置响应头和客户端保存文件名
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(fileName, "UTF-8"));
        InputStream inputStream = null;
        try {
            //打开本地文件流，webapp下file包
            inputStream = new FileInputStream(path+"/statics/"+fileName);
            //激活下载操作
            OutputStream os = response.getOutputStream();
            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.close();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if(inputStream!=null) {
                inputStream.close();
            }
        }

    }

}

